package live.ifox.stage;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

import live.ifox.base.BaseRestActivity;
import live.ifox.factory.Entry;
import live.ifox.factory.StaffInfo;
import live.ifox.factory.Url;
import live.ifox.helper.AdapterHelper;
import live.ifox.helper.CommonHelper;
import live.ifox.helper.CustomProfile;
import live.ifox.util.CommonUtil;

public class LoginActivity extends BaseRestActivity {

    EditText in_name, in_pass;
    String _name, _pass, _pass_encrypt;
    CustomProfile profile;
    boolean registerFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        actionBar.setDisplayHomeAsUpEnabled(false);

        registerFlag = false;
        entry = Entry.Account;
        baseUrl = Url.Account;
        in_name = findViewById(R.id.user_name);
        in_pass = findViewById(R.id.user_pass);
        profile = new CustomProfile(this, "user_profile");

        if (profile.contains("last_name"))
            in_name.setText(profile.getString("last_name"));

        if (profile.contains("last_pass"))
            in_pass.setText(profile.getString("last_pass"));


    }

    public void onRegister_Click(View view) {
        _name = in_name.getText().toString();
        _pass = in_pass.getText().toString();

        if (_name.isEmpty() || _pass.isEmpty()) {
            CommonHelper.showMessage(this,
                    "帐号、密码不能为空！");
        } else {
            registerFlag = true;
            httpGet(baseUrl + "get_account/" + _name);
        }
    }

    public void onSubmit_Click(View view) {
        _name = in_name.getText().toString();
        _pass = in_pass.getText().toString();

        if (_name.isEmpty() || _pass.isEmpty()) {
            CommonHelper.showMessage(this,
                    "帐号、密码不能为空！");
        } else {
            registerFlag = false;
            _pass_encrypt = CommonUtil.encrypt("MD5", _pass);
            httpGet(baseUrl + "get_account/" + _name);
        }
    }

    public void onLogin_Click(View view) {
        profile.putString("user_name", "None");
        profile.putInt("user_level", StaffInfo.Level.User);
        profile.putString("user_group", StaffInfo.Group.None);
        startActivity(new Intent(this,
                MainActivity.class)
                .putExtra("level", StaffInfo.Level.User));
        finish();
    }

    void do_register() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_name", _name);
        map.put("user_pass", CommonUtil.encrypt("MD5", _pass));
        map.put("user_level", StaffInfo.Level.User);
        map.put("user_group", StaffInfo.Group.None);
        httpPost(baseUrl + "insert", JSON.toJSONString(map));
        CommonHelper.showMessage(this, "注册成功！");
    }

    void do_login(JSONObject object) {
        String name = object.getString("user_name");
        String pass = object.getString("user_pass");
        int level = object.getIntValue("user_level");
        String group = object.getString("user_group");

        if (name.equals(_name) && pass.equals(_pass_encrypt)) {
            if (!profile.contains("last_name") ||
                    !profile.getString("last_name").equals(name))
                profile.putString("last_name", name);

            if (!profile.contains("last_pass") ||
                    !profile.getString("last_pass").equals(_pass))
                profile.putString("last_pass", _pass);

            if (!profile.contains("last_level") ||
                    profile.getInt("last_level") != level)
                profile.putInt("last_level", level);

            if (!profile.contains("last_group") ||
                    !profile.getString("last_group").equals(group))
                profile.putString("last_group", group);

            startActivity(new Intent(this,
                    MainActivity.class)
                    .putExtra("level", level));
            finish();
        } else {
            CommonHelper.showMessage(this, "密码错误！");
        }
    }

    @Override
    public void processReply(JSONObject rootObject) {
        JSONArray array = AdapterHelper.verifyObject(rootObject, entry);
        if (array.isEmpty()) {
            if (registerFlag)
                do_register();
            else
                CommonHelper.showMessage(this, "帐号不存在！");
        } else {
            if (registerFlag)
                CommonHelper.showMessage(this, "账号已存在！");
            else
                do_login(array.getJSONObject(0));
        }
    }
}
